package cn.kgc.controller;

import cn.kgc.pojo.Dealer;
import cn.kgc.pojo.User;
import cn.kgc.service.DealerService;
import cn.kgc.util.Page;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.net.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class DealerController {
    @Resource
    private DealerService dealerService;

    @RequestMapping(value = "/queryDealer")
    @ResponseBody
    public String queryAllDealer(String dealerName, String dealerCode,
                                 @RequestParam(defaultValue = "0") Integer pageNum){
        System.out.println(dealerName);
        System.out.println(dealerCode);
        //设置起始页和页大小
        PageHelper.startPage(pageNum,2);
        List<Dealer> list=dealerService.queryAllDealer(dealerName,dealerCode);
        //创建页面信息 对原来的页面进行分页
        PageInfo<Dealer> pageInfo=new PageInfo<Dealer>(list);
        return JSON.toJSONString(pageInfo);

    }
    @RequestMapping(value = "/addDealer")
    public String addDealer(Dealer dealer){
        Integer integer=dealerService.addDealer(dealer);
        if(integer>0){
            return "jsp/dealer/dealer";
        }else{
            return "jsp/dealer/addDealer";
        }
    }
    @RequestMapping(value = "/updateDealer")
    public String updateDealer(Dealer dealer){
        Integer integer=dealerService.updateDealer(dealer);
        if(integer>0){
            return "jsp/dealer/dealer";
        }else{
            return "jsp/dealer/updateDealer";
        }
    }

    @RequestMapping(value = "/queryByIdDealer/{did}")
    public String queryByIdDealer(@PathVariable String did, Model model){
        Dealer dealer=dealerService.queryByIdDealer(did);
        if(dealer!=null){
            model.addAttribute("dealer",dealer);
            return "jsp/dealer/updateDealer";
        }else{
            return "jsp/dealer/dealer";
        }

    }

    @RequestMapping(value = "/deleteDealer/{did}")
    public String deleteDealer(@PathVariable String did){
        Integer integer=dealerService.deleteDealer(did);

        return "jsp/dealer/dealer";
    }



}
